package com.zjsru.plan2024.oneday;

import java.util.Arrays;

/**
 * 881. 救生艇
 * @Author: cookLee
 * @Date: 2024-06-10
 */
public class NumRescueBoats {

    /**
     * 主
     * \
     * 输入：people = [1,2], limit = 3
     * 输出：1
     * 解释：1 艘船载 (1, 2)
     * \
     * 输入：people = [3,2,2,1], limit = 3
     * 输出：3
     * 解释：3 艘船分别载 (1, 2), (2) 和 (3)
     * \
     * 输入：people = [3,5,3,4], limit = 5
     * 输出：4
     * 解释：4 艘船分别载 (3), (3), (4), (5)
     * \
     * @param args args
     */
    public static void main(String[] args) {
        NumRescueBoats numRescueBoats = new NumRescueBoats();
        int[] people = new int[]{3,2,2,1};
        int limit  = 3;
        System.out.println(numRescueBoats.numRescueBoats(people, limit));
    }

    /**
     * 贪心
     *
     * @param people 人
     * @param limit  限制
     * @return int
     */
    public int numRescueBoats(int[] people, int limit) {
        int ans = 0;
        //按照最优值匹配
        Arrays.sort(people);
        //定义最轻和最重值
        int light = 0, heavy = people.length - 1;
        while(light <= heavy){
            if(people[light] + people[heavy] <=  limit){
                light ++;
            }
            heavy--;
            ans++;
        }
        return ans;
    }

}
